/**
 * <AiRmob-Framework; A simple augmented reality framework for android >
    Copyright (C) <2012>  <AiRmob Team>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package airmob.framework;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;

/**
 * 
 * @author nlichtenberg
 * 
 */
public class GPSAlertCreator {

	
	AiRmobActivity mActivity;	
	public List<PendingIntent> pIntentList = new ArrayList<PendingIntent>();
	
	public GPSAlertCreator(AiRmobActivity activity){
		mActivity = activity;
	}
	
	
	/**
	 * Creates a proximityAlert for each GPSMarker in the given resourcefile
	 * @param filename
	 */
	public void addMultipleGPSMarkerAlerts(String filename){
		HashMap<String, GPSMarker> markerMap = null;
		try {
			markerMap = AiRmobFileManager.loadGPSMarker(filename);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		Iterator it = markerMap.entrySet().iterator();
	    while (it.hasNext()) {
	        HashMap.Entry pairs = (HashMap.Entry)it.next();
	        addGPSMarkerAlert(markerMap.get(pairs.getKey()));
	        it.remove(); // avoids a ConcurrentModificationException
	    }
	}

	/**
	 * Creates a proximityAlert for the given GPSMarker
	 * @param marker
	 */
	public void addGPSMarkerAlert(GPSMarker marker){	
		
		Intent intent = new Intent(marker.markerID);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(mActivity.getApplicationContext(), 0, intent, 0);
        pIntentList.add(proximityIntent);
     
        mActivity.locationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE);
        mActivity.locationManager.addProximityAlert(
            marker.latitude, // the latitude of the central point of the alert region
            marker.longitude, // the longitude of the central point of the alert region
            marker.radius, // the radius of the central point of the alert region, in meters
            -1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
            proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
       );
       IntentFilter filter = new IntentFilter(marker.markerID);  
       mActivity.registerReceiver(new GPSMarkerIntentReceiver(marker.markerID,mActivity), filter);
	}
	
	
	/**
	 * removes all alerts created by the calling object and stops GPS
	 */
	public void removeAllAlerts(){
		if(pIntentList.size() > 0){
			int j = pIntentList.size();
			for(int i = 0; i < j; i++){
				mActivity.locationManager.removeProximityAlert(pIntentList.get(i));
			}
		}
	
}


	class GPSMarkerIntentReceiver extends BroadcastReceiver{
		
		String mMarkerID = null;
		AiRmobActivity mActivity;
		
		public GPSMarkerIntentReceiver(String markerID, AiRmobActivity activity) {			
			mMarkerID = markerID;
			mActivity = activity;			
		}
		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
		
			String key = LocationManager.KEY_PROXIMITY_ENTERING;	 
			Boolean entering = intent.getBooleanExtra(key, false);
			
			mActivity.onReceiveMarker(mMarkerID, entering);
		}
	
	}
}
